﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Relocation.Com
{
	/// <summary>
	/// 此类用于设置控件的相关属性
	/// </summary>
	public class ControlTag
	{
		#region 构造函数
		/// <summary>
		/// 构造函数
		/// </summary>
		public ControlTag()
		{
		}


		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="fieldName">字段名称</param>
		public ControlTag(string fieldName)
		{
			this.FieldName = fieldName;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="fieldName">字段名称</param>
		public ControlTag(string fieldName,object defaultValue)
		{
			this.FieldName = fieldName;
			this.DefaultValue = defaultValue;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="fieldName">字段名称</param>
		/// <param name="validatorInfo">验证信息</param>
		public ControlTag(string fieldName, ValidatorInfo validatorInfo)
			: this(fieldName)
		{
			this.ValidatorInfo = validatorInfo;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="fieldName">字段名称</param>
		/// <param name="validatorInfo">验证信息</param>
		/// <param name="searchInfo">搜索信息</param>
		public ControlTag(string fieldName, ValidatorInfo validatorInfo, SearchInfo searchInfo)
			: this(fieldName, validatorInfo)
		{
			this.SearchInfo = searchInfo;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="fieldName">字段名称</param>
		/// <param name="validatorInfo">验证信息</param>
		/// <param name="searchInfo">搜索信息</param>
		/// <param name="roleInfo">角色信息</param>
		public ControlTag(string fieldName, ValidatorInfo validatorInfo, SearchInfo searchInfo, RoleInfo roleInfo)
			: this(fieldName, validatorInfo, searchInfo)
		{
			this.RoleInfo = roleInfo;
		}


		/// <summary>
		/// 角色信息
		/// </summary>
		/// <param name="roleInfo"></param>
		public ControlTag(RoleInfo roleInfo)
		{
			this.RoleInfo = roleInfo;
		}
		
		#endregion
		
		/// <summary>
		/// 字段名称（实体的属性）
		/// </summary>
		private string _FieldName;
		/// <summary>
		/// 字段名称（实体的属性）
		/// </summary>
		public string FieldName
		{
			set
			{
				_FieldName = value;
			}
			get
			{
				return String.IsNullOrEmpty(_FieldName) ? null : _FieldName.Trim();
			}
		}

		/// <summary>
		/// 验证信息配置
		/// </summary>
		public ValidatorInfo ValidatorInfo;

		/// <summary>
		/// 搜索信息配置
		/// </summary>
		public SearchInfo SearchInfo;

		/// <summary>
		/// 权限信息
		/// </summary>
		public RoleInfo RoleInfo;

		/// <summary>
		/// 默认值
		/// </summary>
		public object DefaultValue;

		/// <summary>
		/// 空值
		/// </summary>
		public object NullValue;


	
	}

	/// <summary>
	/// 控件的验证信息配置
	/// </summary>
	public class ValidatorInfo
	{

		public ValidatorInfo()
		{
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="isRequired">是否必填，验证类型默认为String</param>
		public ValidatorInfo(bool isRequired)
		{
			this.IsRequired = isRequired;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="isRequired">是否必填</param>
		/// <param name="validatorType">验证类型</param>
		public ValidatorInfo(bool isRequired, ValidatorTypes validatorType)
			: this(isRequired)
		{
			this.ValidatorType = validatorType;
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="validator">自定义验证的方法委托</param>
		public ValidatorInfo(ValidatorHandler validator)
		{
			this.Validator = validator;
		}

		#region 验证

		public bool IsValidatoField;

		/// <summary>
		/// 是否是必填
		/// </summary>
		public bool IsRequired;

		/// <summary>
		/// 最小长度（验证字符串）
		/// </summary>
		public int MaxLength;

		/// <summary>
		/// 最小长度（验证字符串）
		/// </summary>
		public int MinLength;

		/// <summary>
		/// 自定义验证的正则表达式字符串
		/// </summary>
		public string RuleString;

		/// <summary>
		/// 自定义正则验证失败的提示文本
		/// </summary>
		public string RuleErrorString;

		/// <summary>
		/// 验证的类型
		/// </summary>
		public ValidatorTypes ValidatorType;

		/// <summary>
		/// 验证类型的枚举
		/// </summary>
		public enum ValidatorTypes : sbyte
		{
			/// <summary>
			/// 字符串
			/// </summary>
			String,
			/// <summary>
			/// 整数
			/// </summary>
			Number,
			/// <summary>
			/// 数值（可以有小数）
			/// </summary>
			Digit,
			/// <summary>
			/// 日期
			/// </summary>
			DateTime,
			/// <summary>
			/// 身份证（15位或18位）
			/// </summary>
			Idcards,
			/// <summary>
			/// 身份证（15位）
			/// </summary>
			Idcard15,
			/// <summary>
			/// 身份证（18位）
			/// </summary>
			Idcard18
		};

		/// <summary>
		/// 自定义验证方法
		/// </summary>
		public ValidatorHandler Validator;

		/// <summary>
		/// 自定义的验证方法的委托
		/// </summary>
		/// <param name="control"></param>
		/// <returns></returns>
		public delegate ValidatorResult ValidatorHandler(Control control);

		/// <summary>
		/// 验证结果结构
		/// </summary>
		public class ValidatorResult
		{
			/// <summary>
			/// 是否验证成功
			/// </summary>
			public bool Success
			{
				get;
				set;
			}
			/// <summary>
			/// 验证不成功的错误文本
			/// </summary>
			public string ErrorMessage
			{
				get;
				set;
			}

			/// <summary>
			/// 验证结果构造函数
			/// </summary>
			/// <param name="success">是否成功</param>
			public ValidatorResult(bool success)
			{
				this.Success = success;
				this.ErrorMessage = "";
			}

			/// <summary>
			/// 验证结果构造函数
			/// </summary>
			/// <param name="success">是否成功</param>
			/// <param name="errorMessage">错误信息</param>
			public ValidatorResult(bool success, string errorMessage)
			{
				this.Success = success;
				this.ErrorMessage = String.IsNullOrEmpty(errorMessage) ? "" : errorMessage;
			}
		}
		#endregion
	}

	/// <summary>
	/// 搜索控件的配置信息
	/// </summary>
	public class SearchInfo
	{
		public SearchInfo()
		{ 
		
		}

		/// <summary>
		/// 默认的SearchFieldType为SearchFieldTypes.String
		/// </summary>
		/// <param name="searchField"></param>
		public SearchInfo(String searchField)
		{
			this.IsSearchField = true;
			this.SearchField = searchField;
			this.SearchFieldType = SearchFieldTypes.String;
		}

		public SearchInfo(String searchField, SearchFieldTypes searchFieldType)
			: this(searchField)
		{			
			this.SearchFieldType = searchFieldType;
		}

		public SearchInfo(String searchField, SearchFieldTypes searchFieldType, RanderHandler rander)
			: this(searchField, searchFieldType)
		{
			this.Rander = rander;
		}

		public SearchInfo(String searchField, SearchFieldTypes searchFieldType, RanderHandler rander, ReseterHandler reset)
			: this(searchField, searchFieldType, rander)
		{
			this.Reseter = reset;
		}

		/// <summary>
		/// 自定义取值的委托
		/// </summary>
		/// <param name="control">控件</param>
		/// <returns></returns>
		public delegate string RanderHandler(Control control);

		/// <summary>
		/// 自定义清空搜索条件的委托
		/// </summary>
		/// <param name="control">控件</param>
		/// <returns></returns>
		public delegate void ReseterHandler(Control control);

		#region 搜索标志
		/// <summary>
		/// 是否是搜索字段
		/// </summary>
		public bool IsSearchField;

		/// <summary>
		/// 搜索字段的字段名称
		/// </summary>
		public string SearchField;

		/// <summary>
		/// 搜索字段的数据类型（默认为String）
		/// </summary>
		public SearchFieldTypes SearchFieldType = SearchFieldTypes.String;

		/// <summary>
		/// 自定义取值的委托
		/// </summary>
		public RanderHandler Rander;

		/// <summary>
		/// 重置搜索条件的委托
		/// </summary>
		public ReseterHandler Reseter;

		/// <summary>
		/// 搜索字段的数据类型的枚举
		/// </summary>
		public enum SearchFieldTypes : sbyte
		{
            /// <summary>
            /// 字符串模糊匹配
            /// </summary>
			String,
            /// <summary>
            /// 数值类型匹配
            /// </summary>
			Number,
            /// <summary>
            /// 日期类型匹配
            /// </summary>
			DateTime,
            /// <summary>
            /// 字符串从左匹配
            /// </summary>
            LeftString,
            /// <summary>
            /// 字符串从右匹配
            /// </summary>
            RightString
		};
		#endregion

	}

	public class RoleInfo
	{
		private List<string> _ShowInRoles = new List<string>();

		public List<string> ShowInRoles
		{
			get
			{
				return _ShowInRoles;
			}
			set
			{
				_ShowInRoles = value;
			}
		}

		public RoleInfo(params string[] roles)
		{
			ShowInRoles.AddRange(roles);			
		}

	}
}
